// 商品相关API调用 - 与后端ProductController接口保持一致

import request from '@/utils/request'
import type {
  Product,
  ProductQueryParams,
  ProductPageResponse,
  BatchStockRequest,
  CloneRequest,
  ProductBundleRequest
} from '@/types/product'

// 创建商品
export const createProduct = (data: Product) => {
  return request.post<number>('/api/products', data)
}

// 更新商品
export const updateProduct = (id: number, data: Product) => {
  return request.put<boolean>(`/api/products/${id}`, data)
}

// 商品详情
export const getProductDetail = (id: number) => {
  return request.get<Product>(`/api/products/${id}`)
}

// 商品列表（分页查询，支持关键词、品牌、分类、状态、热门等过滤）
export const getProductList = (params: ProductQueryParams) => {
  return request.get<ProductPageResponse>('/api/products', { params })
}

// 根据品牌ID查询商品
export const getProductsByBrand = (brandId: number, status?: number) => {
  const params = status !== undefined ? { status } : {}
  return request.get<Product[]>(`/api/products/brand/${brandId}`, { params })
}

// 获取品牌下的商品数量
export const getBrandProductCount = (brandId: number) => {
  return request.get<ProductPageResponse>('/api/products', {
    params: {
      brandId,
      status: 1, // 只统计上架商品
      page: 1,
      size: 1 // 只要第一页的一条数据，主要是为了获取total
    }
  }).then(response => response.data.total)
}

// 根据分类ID查询商品
export const getProductsByCategory = (categoryId: number, status?: number) => {
  const params = status !== undefined ? { status } : {}
  return request.get<Product[]>(`/api/products/category/${categoryId}`, { params })
}

// 更新商品状态（上下架）
export const updateProductStatus = (id: number, status: number) => {
  return request.patch<boolean>(`/api/products/${id}/status`, null, {
    params: { status }
  })
}

// 更新热门标记
export const updateProductHot = (id: number, isHot: number) => {
  return request.patch<boolean>(`/api/products/${id}/hot`, null, {
    params: { isHot }
  })
}

// 更新库存
export const updateProductStock = (id: number, stock: number) => {
  return request.patch<boolean>(`/api/products/${id}/stock`, null, {
    params: { stock }
  })
}

// 更新价格
export const updateProductPrice = (id: number, price: number) => {
  return request.patch<boolean>(`/api/products/${id}/price`, null, {
    params: { price }
  })
}

// 逻辑删除商品
export const deleteProduct = (id: number) => {
  return request.delete<boolean>(`/api/products/${id}`)
}

// 扣减库存
export const deductStock = (id: number, quantity: number) => {
  return request.post<boolean>(`/api/products/${id}/${quantity}`)
}

// 回滚库存
export const rollbackStock = (id: number, quantity: number) => {
  return request.post<boolean>(`/api/products/rollback/${id}/${quantity}`)
}

// 批量删除商品
export const batchDeleteProducts = (productIds: number[]) => {
  return request.delete<string>('/api/products/batch', { data: productIds })
}

// 批量更新库存
export const batchUpdateStock = (data: BatchStockRequest) => {
  return request.post<string>('/api/products/batch/stock', data)
}

// 商品克隆
export const cloneProduct = (id: number, data: CloneRequest) => {
  return request.post<string>(`/api/products/${id}/clone`, data)
}

// 创建商品组合
export const createProductBundle = (data: ProductBundleRequest) => {
  return request.post<string>('/api/products/bundle', data)
}

// 导出商品数据
export const exportProducts = (params: Partial<ProductQueryParams>) => {
  return request.get('/api/products/export', {
    params,
    responseType: 'blob'
  })
}

// 下载导入模板
export const downloadImportTemplate = () => {
  return request.get('/api/products/template', {
    responseType: 'blob'
  })
}

// 导入商品数据
export const importProducts = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return request.post<string>('/api/products/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
} 